//
//  RoadsideAssistanceMerchandiseCell.m
//  JxCarios
//
//  Created by xcq on 15/2/1.
//  Copyright (c) 2015年 xiongchuanqi. All rights reserved.
//

#import "RoadsideAssistanceMerchandiseCell.h"

#define CELLHEIGHT 40
static NSString *cellDay = @"roadside";
static NSString *cellNight = @"RoadsideNight";
@implementation RoadsideAssistanceMerchandiseCell
{
    
}

- (void)awakeFromNib {
     _mainRect = [RextExtends getScreenFrame];
    _page = 1;
     [_firstTableView registerNib:[UINib nibWithNibName:@"RoadsideAssistanceDayCell" bundle:nil] forCellReuseIdentifier:cellDay];
    [_secendTableView registerNib:[UINib nibWithNibName:@"RoadsideAssistanceNightTableCell" bundle:nil] forCellReuseIdentifier:cellNight];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(firstBlock)];
    [_firstBackView addGestureRecognizer:tap];
    UITapGestureRecognizer *tap_two = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(secendBlock)];
    [_secondBackView addGestureRecognizer:tap_two];
    [_baitianBtn addTarget:self action:@selector(firstBlock) forControlEvents:UIControlEventTouchUpInside];
    [_wanshangBtn addTarget:self action:@selector(secendBlock) forControlEvents:UIControlEventTouchUpInside];
    _firstTableView.tableFooterView = [UIView new];
    _secendTableView.tableFooterView = [UIView new];
}

- (void)firstBlock {
    //点击滚到第二页
    _page = 2;
    [self setRoadTime:NO];
    [_scrollView setContentOffset:CGPointMake(_mainRect.size.width, 0) animated:YES];
}

- (void)secendBlock {
    //点击滚到第一页
    _page = 1;
    [self setRoadTime:YES];
    [_scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}

- (void)setFirstDic:(NSDictionary *)firstDic{
    if (firstDic && [[firstDic customObjectForkey:@"goods"] count] > 0) {
 
        _firstDic = firstDic;
        [self setRoadTime:YES];
        [_firstTableView reloadData];
    }
}


- (void)setSecondDic:(NSDictionary *)secondDic{
    if (secondDic && [[secondDic customObjectForkey:@"goods"] count] > 0) {
      
        _secondDic = secondDic;
        [_secendTableView reloadData];
        
    }
}

- (void)setRoadTime:(BOOL)bol{
    if (bol) {
        if ([[_firstDic objectForKey:@"time"] isEqualToString:@""]) {
 
        }else {
            _roadsideTime.text = [_firstDic objectForKey:@"time"];

        }
        _roadsideLabel.text = @"白天路救";
    }else{
        if ([[_secondDic objectForKey:@"time"]isEqualToString:@""]) {
            
            
        }else {
            _roadsideTime.text = [_secondDic objectForKey:@"time"];
        }
        _roadsideLabel.text = @"夜间路救";
    }
}

- (CGFloat)height{
    if ([[_firstDic objectForKey:@"goods"] count]> [[_secondDic objectForKey:@"goods"] count]) {
        return  CELLHEIGHT + (CELLHEIGHT * [[_firstDic objectForKey:@"goods"] count]);
    }else{
        return  CELLHEIGHT + (CELLHEIGHT * [[_secondDic objectForKey:@"goods"] count]);

    }
}


#pragma mark - 点击事件代理方法
- (void)checkCallBack:(UITableViewCell *)cell andSeleted:(BOOL)seleted{
    
    if([_delegate respondsToSelector:@selector(checkRoadSideCallBack:andSeleted:)]){
        [_delegate checkRoadSideCallBack:cell andSeleted:seleted];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_page == 1) {
        RoadsideAssistanceDayCell *cell = (RoadsideAssistanceDayCell *)[tableView cellForRowAtIndexPath:indexPath];
        cell.checkBtn.selected = !cell.checkBtn.selected;
        cell.selete = cell.checkBtn.selected;
        if([_delegate respondsToSelector:@selector(checkRoadSideCallBack:andSeleted:)]){
            [_delegate checkRoadSideCallBack:cell  andSeleted:cell.selete];
        }
    }
   
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == _firstTableView) {
         return [[_firstDic objectForKey:@"goods"] count] > 0 ? [[_firstDic objectForKey:@"goods"] count] : 0;
    }else{
        return [[_secondDic objectForKey:@"goods"] count] > 0 ? [[_secondDic objectForKey:@"goods"] count] :0;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == _firstTableView) {
 
        RoadsideAssistanceDayCell *cell = [tableView dequeueReusableCellWithIdentifier:cellDay forIndexPath:indexPath];
        cell.delegate = self;
         cell.info = [[_firstDic objectForKey:@"goods"] objectAtIndex:indexPath.row];
         return cell;
    }else{
 
        NSString *str = @"￥";
        NSDictionary *dic = [[_secondDic objectForKey:@"goods"] objectAtIndex:indexPath.row];
        RoadsideAssistanceNightTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellNight forIndexPath:indexPath];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        NSLog(@"%@",[dic customObjectForkey:@"GOOD_NAME"]);
        cell.shopName.text = [dic customObjectForkey:@"GOOD_NAME"];
        cell.shopPrice.text = [str stringByAppendingString:[dic customObjectForkey:@"CHEAP_PRICE"]];
        return cell;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return CELLHEIGHT;
}


@end
